package src;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.awt.print.Book;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DOM4JTest {
    private static ArrayList<Book> bookList=new ArrayList<Book>();

    public static void main(String[] args) throws DocumentException {
        SAXReader reader=new SAXReader();
        Document document =reader.read(new File("xmlStr.XML"));
        Element bookStore=document.getRootElement();
        Iterator it=bookStore.elementIterator();
        while (it.hasNext()){


            System.out.println("开始遍历列表");
            Element book=(Element) it.next();
            //获取book属性名以及属性值
            List<Attribute> bookAttrs=book.attributes();
            for (Attribute attr : bookAttrs) {
                System.out.println("属性名：" + attr.getName() + "--属性值："
                        + attr.getValue());
            }
            Iterator itt = book.elementIterator();
            while (itt.hasNext()) {
                Element bookChild = (Element) itt.next();
                System.out.println("节点名：" + bookChild.getName() + "--节点值：" + bookChild.getStringValue());
            }
            System.out.println("=====结束遍历列表=====");
        }
    }

    }


